﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Anagramme.Model;
using Anagramme.Contracts;
using System.ApplicationModel;
using Anagramme.Views;
using System.Windows.Input;
using System.ApplicationModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows;
using Anagramme.Services;
using System.Activities;
using Anagramme.Activities.BusinessRules;

namespace Anagramme.ViewModels
{
    /// <summary>
    /// ViewModel des résultats
    /// </summary>
    [ViewModelExport(Modules.Results)]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public class ResultsViewModel : ViewModelBase<IResultsModel>
    {
        #region Contructors
        /// <summary>
        /// Crée une nouvelle instance de la classe <see cref="ResultsViewModel"/>
        /// </summary>
        public ResultsViewModel()
        {
            base.BeginWaitingAction();
            WeakEventManager<IResultsModel, EventArgs<ResultInfo>>.AddHandler(base.Model, "OnResponseResultInfos", this.Model_OnResponseResultInfos);
            base.Model.RequestResultInfos();
        }
        #endregion

        #region Fields
        private Result _result;

        private ICommand _goHomeCommand;
        #endregion

        #region Public Properties
        /// <summary>
        /// Obient les informations sur le résultat
        /// </summary>
        public Result Result
        {
            get { return this._result; }
            private set
            {
                if (!object.ReferenceEquals(this._result, value))
                {
                    base.RaisePropertyChanging();
                    this._result = value;
                    base.TraceAssign(value);
                    base.RaisePropertyChanged();
                }
            }
        }
        #endregion

        #region Commands
        /// <summary>
        /// Obtient la commande pour retourner à la page d'accueil
        /// </summary>
        public ICommand GoHomeCommand
        {
            get
            {
                if (object.ReferenceEquals(this._goHomeCommand, null))
                {
                    this._goHomeCommand = new DelegateCommand(this.GoHome, this.CanGoHome);
                }
                return this._goHomeCommand;
            }
        }
        #endregion

        #region Model Callbacks
        private void Model_OnResponseResultInfos(object sender, EventArgs<ResultInfo> e)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            if (object.ReferenceEquals(e.Value.Winner, null))
            {
                ApplicationServices.Trace.TraceData("winnerid", null);
                Result = Anagramme.Model.Result.Equality;
            }
            else
            {
                int winnerid = e.Value.Winner.Id;
                ApplicationServices.Trace.TraceData("winnerid", winnerid);

                if (Player.Team.Id.Equals(winnerid))
                {
                    this.Result = Anagramme.Model.Result.Win;
                }
                else
                {
                    this.Result = Anagramme.Model.Result.Lose;
                }
            }
            base.EndWaitingAction();

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        #endregion

        #region Methods
        private bool CanGoHome()
        {
            ApplicationServices.Trace.TraceMethodEntering();

            Dictionary<string, object> arguments = new Dictionary<string, object>()
            {
                { "IsBusy", base.IsBusy }
            };

            bool returnValue = (bool)WorkflowInvoker.Invoke(new NotBusyActivity(), arguments)["ReturnValue"];

            ApplicationServices.Trace.TraceReturnValue(returnValue); 
            ApplicationServices.Trace.TraceMethodLeaving();
            return returnValue;
        }
        
        private void GoHome()
        {
            ApplicationServices.Trace.TraceMethodEntering();

            IViewWorkflowManager workflowManager = ApplicationServices.Container.GetExportedValue<IViewWorkflowManager>();
            workflowManager.SendMessage(Messages.ResultsGohome);

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        #endregion
    }
}
